#include "downquick.h"
#include "ui_downquick.h"
#include<QPainter>
#include<QMessageBox>
#include<widget.h>

#define TIMEOUT 200

DownQuick::DownQuick(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::DownQuick)
    ,myTimerId(0)
{

    ui->setupUi(this);
    for (int i = 0; i < 7; ++i) {
        QVector<int> row;
        for (int j = 0; j < 7; ++j) {
            row.append(0); // 假设所有格子都是空地
            // ... 你可以根据需要设置不同的值
        }
    gameMatrix.append(row);
    }


    // 加载图片资源（假设已经在Qt资源文件中）
    emptyPixmap.load("://rsc/empty.png");
    spikesPixmap.load("://rsc/spikes.png");
    stairPixmap.load("://rsc/stair.png");
    foodPixmap.load("://rsc/food.png");
    // ... 加载其他图片资源
}

DownQuick::~DownQuick()
{
    delete ui;
}

void DownQuick::setPosition(int x,int y){
    npc_X=x;
    npc_Y=y;
    npc_Rect.moveTo(x,y);
}


void DownQuick::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    for (int i = 0; i < 7; ++i) {
        for (int j = 0; j < 7; ++j) {
            QPixmap pixmap = getPixmapForCell(gameMatrix[i][j]);
            painter.drawPixmap(40 + 70*i, 100 + 70*j, pixmap);
        }
    }
}


std::vector<std::vector<int>> DownQuick::initMatrix() {
    std::srand(static_cast<unsigned int>(std::time(nullptr))); // 注意：这通常只应该在程序开始时设置一次
    int size = 7;
    int numOnes = 7;
    int numTwos = 1;
    int numThrees = 2;
    std::vector<std::vector<int>> matrix = HelpFunction().randomfillMatrix(size, numOnes, numTwos,numThrees);
    return matrix;
}


QPixmap DownQuick::getPixmapForCell(int value) {
    switch (value) {
    case 0: return emptyPixmap;
    case 1: return spikesPixmap;
    case 2: return stairPixmap;
    case 3: return foodPixmap;
    // ... 处理其他值
    default: return QPixmap(); // 或者返回一个错误提示的图片
    }
}

void DownQuick::keyPressEvent(QKeyEvent *k)
{
    if( k->key()==Qt::Key_K)
    {
        if(SpaceTickTwice==0){
        SpaceTickTwice=1;
        }else
        {
            gameMatrix =HelpFunction().convertMatrix( initMatrix());
            QPainter painter(this);
            painter.fillRect(this->rect(), Qt::white);
            for (int i = 0; i < 7; ++i) {
                for (int j = 0; j < 7; ++j) {
                    QPixmap pixmap = getPixmapForCell(gameMatrix[i][j]);
                    painter.drawPixmap(40 + 70*i, 100 + 70*j, pixmap);
                }
            }
            myTimerId=this->startTimer(TIMEOUT);
            picID=2;

            QString path("D:\\Code\\Qt\\DrChen\\rsc\\npc\\idle\\");
            path+=QString::number(Widget::npcID);
            path+="idle_";
            path+=QString::number(picID);
            path+=".png";

            QPixmap pix(path);
            QPixmap scaledPix = pix.scaled(40, 60, Qt::KeepAspectRatio, Qt::SmoothTransformation);
            ui->npcLabel->setPixmap(scaledPix);
            ui->npcLabel->move(270,310);


        }

    }

    if(k->key()==Qt::Key_S)
    {
        if(ui->npcLabel->y()<=450)
        {
            int temp_x=ui->npcLabel->x();
            int temp_y=ui->npcLabel->y();
            temp_y+=70;
            ui->npcLabel->move(temp_x,temp_y);
        }
    }

    if(k->key()==Qt::Key_W)
    {
        if(ui->npcLabel->y()>=120)
        {
            int temp_x=ui->npcLabel->x();
            int temp_y=ui->npcLabel->y();
            temp_y-=70;
            ui->npcLabel->move(temp_x,temp_y);
        }
    }
    if(k->key()==Qt::Key_D)
    {
        if(ui->npcLabel->x()<=450)
        {
            int temp_x=ui->npcLabel->x();
            int temp_y=ui->npcLabel->y();
            temp_x+=70;
            ui->npcLabel->move(temp_x,temp_y);
        }
    }
    if(k->key()==Qt::Key_A)
    {
        if(ui->npcLabel->x()>=120)
        {
            int temp_x=ui->npcLabel->x();
            int temp_y=ui->npcLabel->y();
            temp_x-=70;
            ui->npcLabel->move(temp_x,temp_y);
        }
    }
    if(k->modifiers()==Qt::ControlModifier&& k->key()==Qt::Key_B)
    {
        //backActionSlot();
    }

}



void DownQuick::mousePressEvent(QMouseEvent *m)
{
    QPoint pt=m->pos();
    qDebug()<<pt;

    if(m->button()==Qt::LeftButton)
    {
        qDebug()<<"左键";

    }

    if(m->button()==Qt::RightButton)
    {
        qDebug()<<"右键";

    }
}


void DownQuick::on_dqStartButton_clicked()
{
    this->update();



    gameMatrix =HelpFunction().convertMatrix( initMatrix());
    QPainter painter(this);
    painter.fillRect(this->rect(), Qt::white);
    for (int i = 0; i < 7; ++i) {
        for (int j = 0; j < 7; ++j) {
            QPixmap pixmap = getPixmapForCell(gameMatrix[i][j]);
            painter.drawPixmap(40 + 70*i, 100 + 70*j, pixmap);
        }
    }
    myTimerId=this->startTimer(TIMEOUT);
    picID=2;

    QString path(":/rsc/npc/idle/");
    path+=QString::number(Widget::npcID);
    path+="idle_";
    path+=QString::number(picID);
    path+=".png";

    QPixmap pix(path);
    QPixmap scaledPix = pix.scaled(40, 60, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->npcLabel->setPixmap(scaledPix);
    ui->npcLabel->move(270,310);
}

void DownQuick::timerEvent(QTimerEvent *event)
{

    if(event->timerId()!=myTimerId)
        return;

    QString path(":/rsc/npc/idle/");
    path+=QString::number(Widget::npcID);
    path+="idle_";
    path+=QString::number(picID);
    path+=".png";

    QPixmap pix(path);
    QPixmap scaledPix = pix.scaled(40, 60, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->npcLabel->setPixmap(scaledPix);


    picID++;
    if(4==picID)
    {
        picID=1;
    }
}


void DownQuick::on_dqGiveUpButton_clicked()
{
    this->close();
    QMessageBox::information(nullptr,"GAMEOVER","得分    楼层    ");
    Widget *w=new Widget;
    w->show();
}

